﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace AlbonWeb.Manager.StyleManager
{
    public partial class StyleAdd : System.Web.UI.Page
    {
        private readonly BLL.StyleBLL stylebll = new BLL.StyleBLL();
        private readonly BLL.StylePriceBLL stylepricebll = new BLL.StylePriceBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                if (Request["parentID"] != null)
                {
                    ViewState["Back"] = Request.UrlReferrer.ToString();
                    initData(Request["parentID"].ToString());
                }
            }
        }
        //初始化数据
        protected void initData(string parentid)
        {
            if (Request["styleid"] != null)
            {
                //添加按钮改为更新按钮
                Style_Add.Text = "Update";
                DataTable dtStyle=stylebll.getStyleByID(Request["styleid"].ToString());
                //样式名称 
                styleName.Text = dtStyle.Rows[0]["styleName"].ToString();
                //中文名称
                cnName.Text = dtStyle.Rows[0]["cnName"].ToString();
                //选择模式
                for(int i=0;i<ddlMode.Items.Count;i++)
                { 
                    if(ddlMode.Items[i].Text==dtStyle.Rows[0]["selectMode"].ToString())
                    {
                        ddlMode.SelectedIndex = i; 
                        break;
                    }
                }
                //性别
                ddlGender.SelectedValue = dtStyle.Rows[0]["sex"].ToString();
                //价钱
                stylepricebll.getStylePrice(Request["styleid"].ToString());
                //Active  Need buy
                isActive.Checked = bool.Parse(dtStyle.Rows[0]["active"].ToString());
                needBuy.Checked = bool.Parse(dtStyle.Rows[0]["buyClothMark"].ToString());
            }
            //绑定该层下的所有其它项
            string cmdstr = "select * from Style where parentID="+parentid+" order by sequence";
            Commonlib.SqlHelper.BindDropDownList(ddlSequence, "styleName", "sequence", Commonlib.SqlHelper.SqlConnectionString,System.Data.CommandType.Text, cmdstr, null);
            ddlSequence.Items.Insert(0,new ListItem("----insert after----","0"));
            //绑定性别信息
            cmdstr = "select * from Gender";
            Commonlib.SqlHelper.BindDropDownList(ddlGender, "genderName", "ID", Commonlib.SqlHelper.SqlConnectionString, System.Data.CommandType.Text, cmdstr, null);
            ddlGender.Items.Insert(0, new ListItem("-Gender-", "0"));
            if (parentid != "0")
            {
                DataTable dt = stylebll.getStyleByID(parentid);
                parentStyle.Text = dt.Rows[0]["styleName"].ToString();
                if (Request["isleaf"] != null)
                {
                    //如果添加的是样式的叶选项(第三层)，则可以输入价钱和上传图片
                    ddlMode.SelectedValue = dt.Rows[0]["selectMode"].ToString();
                    stylePrice.Enabled = true;
                    FileUpload1.Enabled = true;
                }
                else
                {
                    //如果是第二层的样式，要确定其子选项的选择模式 
                    ddlMode.Enabled = true;
                }
            }
            else
            {
                //如果是第一层的样式 ，没有父样式 ，且价钱，图片，模式都不可用
                parentStyle.Text = "No Parent";
            }
        }
        //添加样式
        protected void Style_Add_Click(object sender, EventArgs e)
        {
            Albon.Model.Style style = new Albon.Model.Style();
            style.parentID=int.Parse(Request["parentID"].ToString());
            style.styleName = styleName.Text.Trim();
            style.cnName = cnName.Text.Trim();
            style.selectMode = ddlMode.SelectedItem.Value;
            style.sexID = int.Parse(ddlGender.SelectedValue);
            style.styleImage = "";
            style.buyClothMark = needBuy.Checked;
            style.active = isActive.Checked;
            style.insertAfter =float.Parse(ddlSequence.SelectedValue);
            int styleid = 0; 
            //添加样式 
            if (Request["styleid"] == null)
            {
                styleid = stylebll.AddStyle(style);
            }
            else
            {
                styleid = int.Parse(Request["styleid"].ToString());
                style.ID = styleid;
                stylebll.UpdateStyle(style);
            }
            if (decimal.Parse(stylePrice.Text.Trim()) != 0)
            { 
                //添加样式价格
                Albon.Model.StylePrice styleprice = new Albon.Model.StylePrice();
                styleprice.styleID = styleid;
                styleprice.stylePrice =decimal.Parse(stylePrice.Text.Trim());
                styleprice.remark = "Add by " + User.Identity.Name ;
                
                stylepricebll.addPrice(styleprice);
            }

            Response.Redirect(ViewState["Back"].ToString());
        }
    }
}